
define(['config/appregister',"jquery"],function(app){
    app.directive("smallunbo",function($interval){
        return {
            templateUrl:"app/business/index/template/smallunbo.html",
            scope:{
                data:'='
            },
            link:function(scope,element,attrs){
                var h=91;
                scope.dotFlag=true;
                //测试数据
                //scope.data.push({logo:'them/imgs/reVideoFour.jpg'});
                scope.smallAdverNum=Math.ceil(scope.data.length/4);

                if(scope.smallAdverNum!=1){
                    $('.SamllimgList>li').css('height',h*(scope.smallAdverNum+1));
                }else{
                    scope.dotFlag=false;
                }

                //将数据分为每4个一组
                scope.range = function(n) {
                    scope.list=[];
                    for(var i=0;i<n;i++){
                        scope.list.push(i);
                    }
                    return scope.list;
                };

                //轮播
                function Play(){
                    var i= 0,j=0;
                    this.move=function(dir,len,height,m){
                        if(m!=undefined){
                            i=m;
                            j=m;
                        }
                        if(dir!=''){
                            if(dir=='next'){i++;j++}
                            if(dir=='pre'){i--;j--}
                        }
                        if(j>len-1){j=0;}
                        if(i>len){i=1;$('.SamllimgList>li').css('marginTop',0)}
                        if(i<0){i=len-1;j=len-1;$('.SamllimgList>li').css('marginTop',-height*len)}
                        $('.dot>span').eq(j).css({'background':"black",'width':14}).siblings().css({'background':"#dedede",'width':8});
                        $('.SamllimgList>li').stop().animate({marginTop:-height*i},600);
                    }
                }

                var bannerPaly=new Play();
                if(scope.smallAdverNum!=1){
                    //小banner自动播放
                    var timer=$interval(function(){
                        bannerPaly.move('next',scope.smallAdverNum,h);
                    },3500);

                    //切换页面清除潜在定时器
                    scope.$on("$destroy", function() {
                        $interval.cancel( timer );
                    });

                    $('.SamllimgList').hover(function(){
                        $interval.cancel( timer );
                    },function(){
                        timer=$interval(function(){
                            bannerPaly.move('next',scope.smallAdverNum,h);
                        },3500);
                    });
                    //小圆点hover
                    scope.hoverDot=function(m){
                        bannerPaly.move('',scope.smallAdverNum,h,m);
                    }
                }
            }
        }
    });
});
/**
 * Created by 123456 on 2016/10/20.
 */
